const express  = require('express');
const router   = express.Router();
const axios = require('axios');

router.get('/', function (req, res, next) {
	const title = '运营平台-车捕头';
	res.renderPage('index', {
		title: title,
	});
});

router.post('/getAliGpsInfo', function (req, res, next) {
    axios.get(req.body.url).catch(err => {
        console.warn(err)
    }).then(data => {
        let response = data.data;
        let Longitude;
        let Latitude;
        if (response.ExifTag) {
            if (response.GPSLatitude) {
                Longitude = transformTude(transformAli(response.GPSLongitude.value));
                Latitude = transformTude(transformAli(response.GPSLatitude.value));
                res.json({
					success: true,
                    lnglatXY: [Longitude, Latitude],
                    time: (response.DateTime && response.DateTime.value) || '无法获取'
                })
            }
            else {
                res.json({
					success: false,
                    lnglatXY: '未成功获取',
                    time: (response.DateTime && response.DateTime.value) || '无法获取'
                })
            }
        }

    })
})

function transformAli (tude) {
    let arr = /(\d+)deg\s(\d+)\'\s(\d+.\d+)\"/.exec(tude)
    return arr[1] + ', ' + arr[2] + ', ' + arr[3]
}

function transformTude (tude) {
    let rst = tude.split(', ').map(e => Number(e));
    return (rst[0] + rst[1] / 60 + rst[2] / 3600).toFixed(6)
}

module.exports = router;
